<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
session_start();

class Welcome extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->library('upload');
        $this->load->library('pagination');
        $this->load->helper(array('form', 'url'));
        $this->load->model("item_model");
        @session_start();
    }

    public function index() {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model('table_model');
            $data['tables'] = $this->table_model->show_all_table();
            $this->load->view('index', $data);
        }
    }

    public function logout() {
        $this->session->unset_userdata('userid');
        $this->session->unset_userdata('username');
        $this->session->sess_destroy();
        redirect('login/index');
    }

    public function month_reports() {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model('user_model');
            $data['userlist'] = $this->user_model->show_all_user();
           
            $this->load->view('index', $data);
        }
    }

    //--------------------------Table------------------------------------------
    //
	public function tables() {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model("table_model");
            $data["tables"] = $this->table_model->show_all_table();
            $this->load->view('index', $data);
        }
    }

    public function add_tables() {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            if (isset($_REQUEST['submit'])) {
                $name = $this->input->post('table_name', true);
                $type = $this->input->post('type_table', true);
                $this->load->model("table_model");
                if ($this->table_model->checktable('$name') <> 0) {
                    redirect("welcome/add_tables");
                } else {
                    $this->table_model->add_table($name, $type);
                }
                redirect("welcome/tables");
            }

            $this->load->view('index');
        }
    }

    public function edit_tables($id) {

        if (isset($_REQUEST['submit'])) {
            $name = $this->input->post('table_name', true);
            $type = $this->input->post('type_table', true);
            $this->load->model("table_model");
            if ($this->table_model->checktable('$name') <> 0) {
                redirect("welcome/add_tables");
            } else {
                $this->table_model->update_table($id, $name, $type);
            }
            redirect("welcome/tables");
        }
        $this->load->model("table_model");
        $data['tables'] = $this->table_model->load_details($id);
        $this->load->view('index', $data);
    }

    public function del_table($id = null) {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model("table_model");
            $data['tables'] = $this->table_model->del_table($id);
            redirect("welcome/tables");
        }
    }

    //-------------------- ITEM BEGIN-------------------------
    public function items() {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model("item_model");
            $this->load->model("cate_model");
            $this->load->model('unit_model');
            $data['listunit'] = $this->unit_model->show_all_unit();
            $data["listitem"] = $this->item_model->show_all_item();
            $data['listcate'] = $this->cate_model->show_all_cate();
            $this->load->view('index', $data);
        }
    }

    public function add_items() {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            if (isset($_REQUEST['submit'])) {
                $name = $this->input->post('name_items', true);
                $price = $this->input->post('item_price', true);
                $cate = $this->input->post('catetype', true);
                $type_item = $this->input->post('type_item', true);
                $this->load->model("item_model");
                $this->item_model->add_item($name, $price, $cate, $type_item);
                redirect("welcome/items");
            }
            $this->load->model("cate_model");
            $this->load->model('unit_model');
            $data['listunit'] = $this->unit_model->show_all_unit();
            $data['listcate'] = $this->cate_model->show_all_cate();
            $this->load->view('index', $data);
        }
    }

    public function edit_items($id) {

        if (isset($_REQUEST["submit"])) {
            $this->load->model("item_model");
            $name = $this->input->post("name_items", true);
            $price = $this->input->post("item_price", true);
            $cate = $this->input->post('catetype', true);
            $type_item = $this->input->post('type_item', true);
            $this->item_model->edit_item($id, $name, $price, $cate, $type_item);
            redirect("welcome/items");
        }
        $this->load->model("cate_model");
        $this->load->model('unit_model');
        $data['listunit'] = $this->unit_model->show_all_unit();
        $data['listcate'] = $this->cate_model->show_all_cate();
        $data['items'] = $this->item_model->load_details($id);
        $this->load->view('index', $data);
    }

    public function del_item($id) {
        $this->load->model("item_model");
        $this->item_model->del_item($id);
        redirect("welcome/items");
    }

    // -------------------- ITEM END-------------------------
    //-------------------- TAKE ORDER BEGIN-------------------------

    public function orders() {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model('item_model');
            $this->load->model('order_model');
            $this->load->model('user_model');
            if ($this->session->userdata('usertype') == 3) {
                $data['order_list'] = $this->order_model->show_all_order_of_user($this->session->userdata('userid'));
            } else {
                $data['order_list'] = $this->order_model->show_all_order();
            }
			foreach($data['order_list'] as $order_item) {
				if($order_item->root_order > 0) {
					$order_item->root_order_name = $this->order_model->get_root_order_name($order_item->root_order);
				}
			}
// 			var_dump($data['order_list'] );die();
            $data['user_list'] = $this->user_model->show_all_user();
            $this->load->view('index', $data);
        }
    }

    public function del_order($id) {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model('order_model');
            $this->order_model->delete_order_item($id);
            $this->order_model->delete_order($id);
            redirect("welcome/orders");
        }
    }

    public function edit_order($id) {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model('item_model');
            $this->load->model('order_model');
            $data['listitem'] = $this->item_model->show_all_item();
            $data['order_details'] = $this->order_model->load_details($id);
            $data['order_item'] = $this->order_model->show_all_order_item($id);
            $data["total_price"] = $this->order_model->total_price_order($id);
            $data['order_id_num'] = $id;
            if (isset($_REQUEST['submit'])) {
                $name = $this->input->post('name_items', true);
                $items = $this->input->post('orderitems', true);
                $this->load->model("order_model");
                $this->load->model('item_model');
                $orderid = $this->order_model->edit_order($id, $name);
                //$this->order_model->clear_all_order_item($id);
                foreach ($items as $item) {

                    if ($item != null) {
                        $da = explode(",", $item);
                        $this->order_model->edit_order_item($id, $da[1], $this->item_model->return_item_name($da[1]), $this->item_model->return_item_price($da[1]), $da[0]);
                    } else {
                        
                    }
                }
                redirect("welcome/edit_order/" . $id);
            }
            if (isset($_REQUEST['bttback'])) {
                redirect("welcome/orders");
            }
            $this->load->model("cate_model");
            $this->load->model("user_model");
            $this->load->model("order_model");
            $userid_get = $this->order_model->show_username_this_order($id);
            foreach ($this->user_model->show_all_user() as $user) {
                if ($user->id == $userid_get) {
                    $data['user_name'] = $user->username;
                }
            }
            $data['listcate'] = $this->cate_model->show_all_cate();
            $this->load->view('index', $data);
        }
    }

    function check_result_by_month($id, $dulieu) {

        $this->load->model("order_model");
        $values = $this->order_model->show_report_order_by_time($id,$dulieu);
        $total_price = $this->order_model->total_price_by_date($id,$dulieu);
        $month = explode("-", $dulieu);
        $total_price_month = $this->order_model->total_price_by_month($month[1]);
        echo '<table class="table table-striped table-bordered bootstrap-datatable datatable">';
        echo "<thead>";
        echo "<tr>";
        echo "<th>Món đã bán:</th>";
        echo "<th>Số lượng</th> ";
        echo "<th>Giá</th>";
        echo "</tr>";
        echo "</thead>  ";
        echo "<tbody>";
        foreach ($values as $value) {
            echo "<tr>";
            echo "<td>";
            echo $value->item_name;
            echo "</td>";
            echo "<td>";
            echo $value->amount;
            echo "</td>";
            echo "<td>";
            echo number_format($value->total);
            echo "</td>";
        }
        echo "<tr>";
        echo "<td>";
        echo "</td>";
        echo "<td>";
        echo "<b>Tổng bán trong ngày</b>";
        echo "</td>";
        echo "<td>";
        echo "<b>" . number_format($total_price) . "</b>";
        echo "</td>";
        echo "<tr>";
        echo "<td>";
        echo "</td>";
        echo "<td>";
        echo "<b>Tổng bán trong tháng: " . $month[1] . "</b>";
        echo "</td>";
        echo "<td>";
        echo "<b>" . number_format($total_price_month) . "</b>";
        echo "</td>";
        echo "</tbody>";
        echo '</table>';
    }

    public function remove_item_order($item_id, $order_id) {
        $this->load->model('order_model');
        $this->order_model->remove_item_id_in_order($item_id);
        redirect('welcome/edit_order/' . $order_id);
    }

    public function finish_item_order($item_id, $order_id) {
        $this->load->model('order_model');
        $this->order_model->finish_item($item_id, $order_id);
        redirect('welcome/edit_order/' . $order_id);
    }

    public function checkout($id) {
        $this->load->model("order_model");
        $this->order_model->update_checkout_order($id);
        redirect("welcome/orders");
    }

<<<<<<< .mine
    public function checkout_pending($id) {
        $this->load->model("order_model");
        $this->order_model->update_checkout_order_pending($id);
        redirect("welcome/orders");
    }
=======
	public function checkout($id) {
		$this->load->model("order_model");
		$this->order_model->update_checkout_order($id);
		$sub_order = $this->order_model->show_all_sub_order($id);
		if (count($sub_order)>0) {
			foreach ($sub_order as $sub_order_id) {
				$this->order_model->update_checkout_order($sub_order_id);
			}
		}
		redirect("welcome/orders");
	}
>>>>>>> .r28

    public function checkout_multi($oders_id) {
        $this->load->model("order_model");
        foreach (explode(",", $oders_id) as $oder_id) {
            $this->order_model->update_checkout_order($oder_id);
        }
        redirect("welcome/orders");
    }

<<<<<<< .mine
    public function show_cate_ajax($id) {
        $this->load->model('item_model');
        $listitem = $this->item_model->show_all_item_by_cate($id);
        echo '<select name="items_select" id="items_select" onchange="changeitem(this);">';
        echo '<option value="0" selected="selected">------------</option>';
        foreach ($listitem as $item) {
            echo '<option value=' . $item->id . '>' . $item->item . '</option>';
        }
        echo '</select>';
    }
=======
	public function checkout_multi($oders_id,$root_order) {
		$this->load->model("order_model");
		foreach (explode(",", $oders_id) as $oder_id) {
			if ($oder_id== $root_order) {
				$this->order_model->update_checkout_order_pending($oder_id);			
			}
			else {
				$this->order_model->update_checkout_order($oder_id);
				$this->order_model->add_root_order($oder_id,$root_order);
			}		
		}
		redirect("welcome/orders");
	}
>>>>>>> .r28

    public function changeprice($discount, $id) {
        $this->load->model('order_model');
        $total_price = $this->order_model->total_price_order($id);

        echo number_format($total_price - ($total_price * $discount) / 100) . " VND";
    }

    public function add_order($table_id = null, $error = null) {
        $data['notification'] = "nhập bàn";
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model('table_model');
            if ($this->table_model->show_all_table_available_static() == 0) {
                if (isset($_REQUEST['submit'])) {
                    $this->load->model('table_model');
                    $id_table = $this->input->post('name_items', true);
                    if ($id_table == 0) {
                        redirect('welcome/add_order/null');
                    } else {
                        //$price = $this->input->post('item_price', true);
                        $items = $this->input->post('orderitems', true);
                        $name_table = $this->table_model->returntablename($id_table);
                        $this->load->model("order_model");
                        $this->load->model('item_model');
                        $orderid = $this->order_model->add_orders($id_table, $name_table, $this->session->userdata('userid'));
                        foreach ($items as $item) {
                            if ($item != null) {
                                $da = explode(",", $item);
                                $this->order_model->add_order_item($orderid, $da[1], $this->item_model->return_item_name($da[1]), $this->item_model->return_item_price($da[1]), $da[0]);
                            } else {
                                
                            }
                        }
                        redirect("welcome/orders");
                    }
                }
                if (isset($_REQUEST['reset'])) {
                    redirect("welcome/orders");
                }
                $this->load->model('item_model');
                $this->load->model('table_model');
                $data['listitem'] = $this->item_model->show_all_item();
                $data['tables'] = $this->table_model->show_all_table_available();
                if ($table_id != null) {
                    $data['table_id'] = $table_id;
                } else {
                    $data['table_id'] = 0;
                }
                $this->load->model("cate_model");
                $data['listcate'] = $this->cate_model->show_all_cate();

<<<<<<< .mine
                if ($error == "null") {
                    $data['notification'] = "Bạn chưa chọn bàn";
                } else {
                    $data['notification'] = "Nhập bàn";
=======
	public function add_order($table_id = null, $error = null) {
		$data['notification'] = "nhập bàn";
		if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
			redirect("login/index");
		} else {
			$this->load->model('table_model');
			if($this->table_model->show_all_table_available_static() == 0){
				if (isset($_REQUEST['submit'])) {
					$this->load->model('table_model');
					$id_table = $this->input->post('name_items', true);
					if ($id_table == 0) {
						redirect('welcome/add_order/null');
					} else {
						//$price = $this->input->post('item_price', true);
						$items = $this->input->post('orderitems', true);
						$name_table = $this->table_model->returntablename($id_table);
						$this->load->model("order_model");
						$this->load->model('item_model');
						$orderid = $this->order_model->add_orders($id_table, $name_table, $this->session->userdata('userid'));
						foreach ($items as $item) {
							if ($item != null) {
								$da = explode(",", $item);
								$this->order_model->add_order_item($orderid, $da[1], $this->item_model->return_item_name($da[1]), $this->item_model->return_item_price($da[1]), $da[0]);
							} else {

							}
						}
						redirect("welcome/orders");
					}
				}
				if (isset($_REQUEST['reset'])) {
					redirect("welcome/orders");
				}
				$this->load->model('item_model');
				$this->load->model('table_model');
				$data['listitem'] = $this->item_model->show_all_item();
				$data['tables'] = $this->table_model->show_all_table_available();
				if ($table_id != null) {
					$data['table_id'] = $table_id;
				} else {
					$data['table_id'] = 0;
				}
				$this->load->model("cate_model");
				$data['listcate'] = $this->cate_model->show_all_cate();

				if ($error == "null") {
					$data['notification'] = "Bạn chưa chọn bàn";
				} else {
					$data['notification'] = "Nhập bàn";
				}
			} else{
				redirect('welcome/orders');
			}
			$this->load->view('index', $data);
		}
	}

	public function checkout_multi_order() {
		$total_price = 0; 
		if (isset($_REQUEST['submit_multi'])) {
			$this->load->model('item_model');
			$this->load->model('order_model');
			$data['listitem'] = $this->item_model->show_all_item();
			$orderid = $this->input->post('order_id_list');
			$data['order_item'] = array();
			$data['listorder'] = array();
			if ($orderid == false) {
				redirect("welcome/orders");
			} else {
				$data['listorder'] = $this->order_model->load_multi_details($orderid);
				foreach ($orderid as $order) { 
					$total_price = $total_price + $this->order_model->total_price_order($order);
					$data['order_item'] = array_merge($data['order_item'], $this->order_model->show_all_order_item($order));
				} 
				$data['total_price'] = $total_price; 
				$data['orderid'] = implode(",", $orderid);
				$this->load->view("index", $data);
			}
		}
	}
        // -------------------- UNIT BEGIN-------------------------
        public function units(){
            if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
			redirect("login/index");
		} else {
                    $this->load->model('unit_model');
                    $data['list_units'] = $this->unit_model->show_all_unit();
                    $this->load->view("index", $data);
>>>>>>> .r28
                }
            } else {
                redirect('welcome/orders');
            }
            $this->load->view('index', $data);
        }
    }

    public function checkout_multi_order() {
        $total_price = 0;
        if (isset($_REQUEST['submit_multi'])) {
            $this->load->model('item_model');
            $this->load->model('order_model');
            $data['listitem'] = $this->item_model->show_all_item();
            $orderid = $this->input->post('order_id_list');
            $data['order_item'] = array();
            $data['listorder'] = array();
            if ($orderid == false) {
                redirect("welcome/orders");
            } else {
                foreach ($orderid as $order) {
                    $total_price = $total_price + $this->order_model->total_price_order($order);
                    $data['order_item'] = array_merge($data['order_item'], $this->order_model->show_all_order_item($order));
                    $order_detail = $this->order_model->load_details($order);
                    $data['listorder'][] = $order_detail[0];
                }
                $data['total_price'] = $total_price;
                $data['orderid'] = implode(",", $orderid);
                $this->load->view("index", $data);
            }
        }
    }

    // -------------------- UNIT BEGIN-------------------------
    public function units() {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model('unit_model');
            $data['list_units'] = $this->unit_model->show_all_unit();
            $this->load->view("index", $data);
        }
    }

    public function add_units() {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            if (isset($_REQUEST['submit'])) {
                $nameunit = $this->input->post('name_units');
                $this->load->model('unit_model');
                $this->unit_model->add_units($nameunit);
                redirect('welcome/units');
            }
            $this->load->view("index");
        }
    }

    public function edit_units($id = null) {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model('unit_model');
            $data['list_unit'] = $this->unit_model->load_details($id);
            if (isset($_REQUEST['submit'])) {
                $nameunit = $this->input->post('name_units');
                $this->load->model('unit_model');
                $this->unit_model->edit_unit($id, $nameunit);
                redirect('welcome/units');
            }
            $this->load->view("index", $data);
        }
    }

    public function del_units($id = null) {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            $this->load->model('unit_model');
            $this->unit_model->del_units($id);
            redirect('welcome/units');
        }
    }

    // -------------------- TAKE ORDER END-------------------------
    public function cate() {
        $this->load->model("cate_model");
        $data["listcate"] = $this->cate_model->show_all_cate();
        $data["listcateroot"] = $this->cate_model->show_all_cate();
        $this->load->view('index', $data);
    }

    public function del_cate($id) {
        $this->load->model('cate_model');
        $this->cate_model->del_cate($id);
        redirect("welcome/cate");
    }

    public function add_cate() {

        if (isset($_REQUEST['submit'])) {

            $catename = $this->input->post('catename', true);
            $cateroot = $this->input->post('cateroot', true);
            $this->load->model("cate_model");
            $this->cate_model->add_cate($catename, $cateroot);
            redirect("welcome/cate");
        }
        $this->load->model("cate_model");
        $data['listcate'] = $this->cate_model->show_all_cate();
        $this->load->view('index', $data);
    }

    public function edit_cate($id) {
        if (isset($_REQUEST["submit"])) {
            $catename = $this->input->post('catename', true);
            $cateroot = $this->input->post('cateroot', true);
            $this->load->model("cate_model");
            $this->cate_model->edit_cate($id, $catename, $cateroot);
            redirect("welcome/cate");
        }
        $this->load->model("cate_model");
        $data['catedetails'] = $this->cate_model->load_details($id);
        $data['listcate'] = $this->cate_model->show_all_cate_root();
        $this->load->view('index', $data);
    }

    public function reports() {
        $this->load->model('order_model');
        $data['allitemstatus'] = $this->order_model->show_all_orderitem_status();
        $this->load->view('index', $data);
    }

    public function finishorder($id) {
        $this->load->model("order_model");
        $this->order_model->finish_order($id);
        redirect('welcome/reports');
    }

    public function loadajaxorder() {
        //echo "test";
        $this->load->model('order_model');
        $data['allitemstatus'] = $this->order_model->show_all_orderitem_status();
        $data['order_table_name'] = $this->order_model->show_all_order();
        $this->load->view('order_ajax', $data);
    }

    //---------------------USER-------------
    public function users() {
        $this->load->model("user_model");
        $data['listuser'] = $this->user_model->show_all_user();
        $this->load->view('index', $data);
    }

    public function del_user($id) {
        $this->load->model("user_model");
        $data['listuser'] = $this->user_model->del_user($id);
        redirect("welcome/users");
    }

    public function add_user() {
        if (isset($_REQUEST["submit"])) {
            $username = $this->input->post('username', true);
            $password = $this->input->post('userpass', true);
            $type = $this->input->post('type_user', true);
            $this->load->model("user_model");
            $this->user_model->add_user($username, $password, $type);
            redirect("welcome/users");
        }
        $this->load->view('index');
    }

    public function edit_user($id) {
        if ($this->session->userdata('userid') == null && $this->session->userdata('username') == null) {
            redirect("login/index");
        } else {
            if (isset($_REQUEST["submit"])) {
                $password = $this->input->post('userpass', true);
                $type = $this->input->post('type_user', true);
                $this->load->model("user_model");
                $this->user_model->edit_user($id, $password, $type);
                redirect("welcome/users");
            }
            $this->load->model("user_model");
            $data['userdetails'] = $this->user_model->load_details($id);
            $data['login_history'] = $this->user_model->load_login_history($id);
            // 		var_dump($data);die();
            $this->load->view('index', $data);
        }
    }

    public function del_login_history($id, $date) {
        $this->load->model("user_model");
        $this->user_model->del_login_history($id, $date);
        redirect("welcome/edit_user/" . $id);
    }

    //------------------------------------
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */